package com.google.code.avajhelpdesk.pages.users;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.google.code.avajhelpdesk.entities.User;
import com.google.code.avajhelpdesk.pages.BasePage;

@AuthorizeInstantiation({User.ROLE_ADMIN})
public class UserListView extends BasePage {
	
	private static final int ENTITIES_PER_PAGE = 20;

	public UserListView() {
		DataTable<User> dataTable = new DefaultDataTable<User>(
				"users", 
				createColumns(), 
				getDataProvider(), 
				ENTITIES_PER_PAGE);
		
		add(dataTable);
		
		add(new PagingNavigator("paging", dataTable));
	}

	private SortableDataProvider<User> getDataProvider() {
		return new SortableUserDataProvider();
	}

	private List<IColumn<User>> createColumns() {
		List<IColumn<User>> columns = new ArrayList<IColumn<User>>();
		
		columns.add(new AbstractColumn<User>(new Model<String>("")) {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<User>> cellItem,
					String componentId, IModel<User> rowModel) {
				cellItem.add(new ActionPanel(componentId, rowModel));
			}
		});
		
		columns.add(new PropertyColumn<User>(new Model<String>("ID"), "id"));
		
		columns.add(new PropertyColumn<User>(new Model<String>("Username"), "username", "username"));
		columns.add(new PropertyColumn<User>(new Model<String>("First Name"), "firstName", "firstName"));
		columns.add(new PropertyColumn<User>(new Model<String>("Last Name"), "lastName", "lastName"));
		
		columns.add(new PropertyColumn<User>(new Model<String>("Contacts"), "contactInfo"));
		return columns;
	}
	
	@Override
	protected String getPageTitle() {
		return "View user list";
	}
}